package com.sky.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sky.constant.MessageConstant;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.User;
import com.sky.entity.UserStatus;
import com.sky.exception.LoginFailedException;
import com.sky.mapper.UserLoginServiceMapper;
import com.sky.mapper.UserShopCarServiceMapper;
import com.sky.mapper.UserStatusServiceMapper;
import com.sky.properties.WeChatProperties;
import com.sky.service.UserLoginService;
import com.sky.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 符健清
 * @version 1.0
 */
@SuppressWarnings({"all"})
@Service
@Slf4j
public class UserLoginServiceimpl implements UserLoginService {
    @Autowired
    private UserStatusServiceMapper userStatusServiceMapper;
    @Autowired
    private UserLoginServiceMapper userLoginServiceMapper;
    @Autowired
    private WeChatProperties weChatProperties;
    public static final String WEX_URL = "https://api.weixin.qq.com/sns/jscode2session";
    @Autowired
    private UserShopCarServiceMapper userShopCarServiceMapper;

    /**
     * 微信登入
     * @param userLoginDTO
     * @return
     */
    @Override
    public User login(UserLoginDTO userLoginDTO) {
        log.info("userLoginDtoCode:{}",userLoginDTO.getCode());
/**
 * 1调用微信 接口服务 ，获取当前用户的openid
 * 2.判断openid 是否为空，如何空 登入失败 返回异常
 * 3.如何用户是新用户，则自动注册
 * 4。 返回这个用户的对象
 */
        String openid = getOpenid(userLoginDTO.getCode());
        if (openid == null) {
//            则该登入的账号异常
//           抛出异常
            throw new LoginFailedException(MessageConstant.LOGIN_FAILED);
        }
//        判断该用户是否为新用户
        User user = userLoginServiceMapper.login(openid);
        if(user==null){
//            创建新用户
            User users= new User();
            users.setOpenid(openid);
            users.setCreateTime(LocalDateTime.now());
               log.info("users:{}",users);
            userLoginServiceMapper.insertUser(users);
        }
////            修改用户为上线状态
//        uppdateUserStatus(user, StatusConstant.ENABLE);
        User us = userLoginServiceMapper.login(openid);
        return  us  ;
    }
//    调用微信接口 返回openid
    private String getOpenid(String code) {
        Map<String, String> map=new HashMap<>();
        map.put("appid",weChatProperties.getAppid());
        map.put("secret",weChatProperties.getSecret());
        map.put("grant_type","authorization_code");
        map.put("js_code",code);

        String json = HttpClientUtil.doGet(WEX_URL, map);
//       将返回数值类型装成json格式
        JSONObject jsonObject = JSON.parseObject(json);
        String openid = jsonObject.getString("openid");
        return openid;
    }
    //            添加 用户状态表格
    private void  insertUserStatus( User user ,Integer  status){
        //            修改 用户状态表格
        UserStatus userStatus = new UserStatus();
        userStatus.setStatus(status);
        userStatus.setLoginDatetime(LocalDateTime.now());
        userStatus.setUserName(user.getName());
        log.info("userStatus:{}" ,userStatus);
        userStatusServiceMapper.insertUserStatus(userStatus);
    }
    private  void  uppdateUserStatus( User user ,Integer  status){
        //            修改 用户状态表格
        UserStatus userStatus = new UserStatus();
        userStatus.setStatus(status);
        userStatus.setLoginDatetime(LocalDateTime.now());
        userStatus.setUserName(user.getName());
        userStatus.setUserId(user.getId());
        log.info("userStatus:{}" ,userStatus);
        userStatusServiceMapper.updateUserStatus(userStatus);
    }

}
